package pl.rzarajczyk.breaktime.gui;

import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Toolkit;
import org.springframework.beans.factory.annotation.Autowired;
import pl.rzarajczyk.breaktime.utils.localization.Localization;
import pl.rzarajczyk.breaktime.ScreenCorner;
import pl.rzarajczyk.breaktime.State;
import pl.rzarajczyk.breaktime.Updateable;
import pl.rzarajczyk.breaktime.utils.ExtendedJFrame;
import pl.rzarajczyk.breaktime.utils.SwingUtils;
import pl.rzarajczyk.breaktime.utils.Utils;
import pl.rzarajczyk.breaktime.utils.localization.LocalizationPoint;

/**
 *
 * @author rzarajczyk
 */
public class TimerWindow extends ExtendedJFrame implements Updateable{
    private static final long serialVersionUID = 1L;

    private ScreenCorner corner;

    public TimerWindow() {
        initComponents();
    }

    @Override
    public void update(State state) {
        if ( state.isBreakActive() ) {
            onBreakProgress(state.getRemainingTimeSec() * 1000);
        } else {
            onWaitProgress(state.getRemainingTimeSec() * 1000);
        }
    }

    private void onBreakProgress(int timeToFinish) {
        this.timer.setText( Utils.toTimeString(timeToFinish) );
    }

    private void onWaitProgress(int timeToBreak) {
        this.timer.setText( Utils.toTimeString(timeToBreak) );
    }

    @LocalizationPoint
    @Override
    public void setVisible(boolean visible) {
        if ( visible ) {
            int x = 0, y = 0;
            if ( corner == null || corner == ScreenCorner.BOTTOM_RIGHT ) {
                x = SwingUtils.getScreenWidth() - this.getWidth();
                y = SwingUtils.getScreenHeight() - this.getHeight() - getTaskbarHeight();
            } else if ( corner == ScreenCorner.BOTTOM_LEFT ) {
                x = 0;
                y = SwingUtils.getScreenHeight() - this.getHeight() - getTaskbarHeight();
            } else if ( corner == ScreenCorner.TOP_LEFT ) {
                x = 0;
                y = 0;
            } else {
                x = SwingUtils.getScreenWidth() - this.getWidth();
                y = 0;
            }
            this.setLocation(x, y);
        }

        super.setVisible(visible);
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        timer = new javax.swing.JLabel();
        label = new javax.swing.JLabel();

        setTitle("Break time!");

        timer.setText("jLabel1");

        label.setText("MicroBreak begins in: ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addComponent(timer)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label)
                    .addComponent(timer))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel label;
    private javax.swing.JLabel timer;
    // End of variables declaration//GEN-END:variables

    public void setScreenCorner(ScreenCorner corner) {
        this.corner = corner;
    }

    public int getTaskbarHeight() {
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle maxBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
        //        System.out.println("Taskbar height: " + (dim.height - maxBounds.height));
        return (dim.height - maxBounds.height);
    }
}
